package com.util.stream.stream;

import org.junit.Test;

import javax.security.auth.login.AccountException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Description
 * @Author away
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2019/8/28
 *
 * https://www.ibm.com/developerworks/cn/java/j-lo-java8streamapi/index.html
 */
public class StreamDemo {


    /**
     *  peek 没有返回值，实际操作不会改变原有的stream
     *  peak 与map区别 https://www.jianshu.com/p/f8fa2a7920f5
     */
    @Test
    public void peekTest() {
        List<String> collect = Stream.of("one", "two", "three", "four")
                .filter(e -> e.length() > 3)
                .peek(e ->{ System.out.println("Filtered value: " + e);e+="qqq";})//Consumer 没有返回值的 void accept(T t);
                .map(String::toUpperCase)// Function    R apply(T t); 有返回值，返回值将修改Stream的值
//                .peek(e -> System.out.println("Mapped value: " + e))
                .collect(Collectors.toList());
        System.out.println(collect);


        boolean b = Stream.of("one", "two", "three", "four").filter(e -> e.length() < 5)
                .anyMatch(e -> "one".equals(e));
        System.out.println(b);
    }


    /**
     * 将多个输入流转化为一个
     */
    @Test
    public void flatMapTest(){
        Stream<List<Integer>> inputStream = Stream.of(
                Arrays.asList(1, 2), Arrays.asList(3, 4),Arrays.asList(6, 7)
        );

        Stream<Integer> outputStream = inputStream.
                flatMap((childList) -> childList.stream());
        System.out.println(outputStream.collect(Collectors.toList()));
    }


    /**
     * 求两个集合的集合
     */
    @Test
    public void anyMatch(){
        List<InstanceDo> instanceDos = Arrays.asList(
                new InstanceDo("code1", "name1"),
                new InstanceDo("code2", "name2"),
                new InstanceDo("code3", "name3")
        );

        List<InstanceDo> list = Arrays.asList(
                new InstanceDo("code1", "name1"),
                new InstanceDo("code4", "name5"),
                new InstanceDo("code6", "name6")
        );

        List<InstanceDo> collect = instanceDos.stream().filter(
                t -> list.stream().anyMatch(yxqdIns -> yxqdIns.getCode().equals(t.getCode()))
        ).collect(Collectors.toList());
        System.out.println(collect);
    }


    /**
     * 求两个集合的集合
     */
    @Test
    public void allMatch(){
        List<String> list = new ArrayList<>();
//        list.add("a");
        List<Integer> collect = list.stream().map(String::length).collect(Collectors.toList());
        System.out.println(collect);
        boolean b = list.stream().allMatch(t -> t == null && "".equals(t));
        System.out.println(b);
    }



//    @Test
    public void compareListHitData(List<Map<Object, Object>> oneList, List<Map<Object, Object>> twoList) {
        List<Map<Object, Object>> resultList = oneList.stream().map(map -> twoList.stream()
                .filter(m -> Objects.equals(m.get("id"), map.get("id")))
                .findFirst().map(m -> {
                    map.putAll(m);
                    return map;
                }).orElse(null))
                .filter(Objects::nonNull).collect(Collectors.toList());

    }

    @Test
    public void mapToInt(){
        List<String> ss = new ArrayList<>();
        ss.add("aaa");
        ss.add("bbb");
        long count = ss.stream().mapToInt(t -> t.length()).sum();
        System.out.println(count);
    }


    class InstanceDo{
        String code ;
        String name;

        public InstanceDo(String code, String name) {
            this.code = code;
            this.name = name;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return "InstanceDo{" +
                    "code='" + code + '\'' +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
}
